#========================================
# author: changlong.zang
#   mail: zclongpop@163.com
#   date: Thu, 26 Nov 2015, 14:38:05
#========================================
import os, uiTool, scriptTool, files, dataTool
from PyQt4 import QtCore, QtGui
#--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
windowClass, baseClass = uiTool.loadUi(os.path.join(scriptTool.getScriptPath(), 'mocapUI.ui'))

class MocapUI(windowClass, baseClass):
    def __init__(self, parent=uiTool.getMayaWindow()):
        super(MocapUI, self).__init__(parent)
        self.setupUi(self)



    def on_btn_selectPath_clicked(self, args=None):
        if args == None:return
        filePath = files.selectDictionary()
        self.let_filesPath.setText(filePath or self.let_filesPath.text())



    def on_let_filesPath_textChanged(self, path):
        dir_path = u'{0}'.format(path)
        folder_files = [f for f in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, f))]

        if not folder_files:
            self.btn_SetRigPath.setEnabled(False)
            #self.btn_SetMocapPath.setEnabled(False)
            self.btn_SetHumanPath.setEnabled(False)
            return

        completer    = QtGui.QCompleter(folder_files)
        for fpath, lineEdit in zip(folder_files, (self.let_rigFilePath, self.let_humanIKFilePath)):
            lineEdit.setText(fpath)
            lineEdit.setCompleter(completer)

        self.btn_SetRigPath.setEnabled(True)
        #self.btn_SetMocapPath.setEnabled(True)
        self.btn_SetHumanPath.setEnabled(True)


    def on_btn_SetRigPath_clicked(self, args=None):
        if args == None:return
        self.let_rigFilePath.setText(files.selectFile(u'{0}'.format(self.let_filesPath.text())) or self.let_rigFilePath.text())

    def on_btn_SetMocapPath_clicked(self, args=None):
        if args == None:return
        self.let_mocapFilePath.setText(files.selectFile(u'{0}'.format(self.let_mocapFilePath.text()), True) or self.let_mocapFilePath.text())

    def on_btn_SetHumanPath_clicked(self, args=None):
        if args == None:return
        self.let_humanIKFilePath.setText(files.selectFile(u'{0}'.format(self.let_filesPath.text())) or self.let_humanIKFilePath.text())


    def on_let_rigFilePath_textChanged(self, text):
        self.let_rigFilePath.setStyleSheet('')


    def on_let_mocapFilePath_textChanged(self, text):
        self.let_mocapFilePath.setStyleSheet('')


    def on_let_humanIKFilePath_textChanged(self, text):
        self.let_humanIKFilePath.setStyleSheet('')


    def on_btn_switchRigMocap_clicked(self, args=None):
        if args == None:return
        self.__switchText(self.let_rigFilePath, self.let_mocapFilePath)



    def on_btn_switchMocapHuman_clicked(self, args=None):
        if args == None:return
        self.__switchText(self.let_mocapFilePath, self.let_humanIKFilePath)



    def on_btn_start_clicked(self, args=None):
        if args == None:return
        dir_path = u'{0}'.format(self.let_filesPath.text())
        fileList = list()
        #-
        for lineEdit in (self.let_rigFilePath, self.let_mocapFilePath, self.let_humanIKFilePath):
            if lineEdit != self.let_mocapFilePath:
                filePath = os.path.join(dir_path, u'{0}'.format(lineEdit.text()))
            else:
                filePath =  u'{0}'.format(lineEdit.text())

            if not os.path.isfile(filePath):
                lineEdit.setFocus()
                lineEdit.setStyleSheet('QLineEdit{color:rgb(200, 0, 0);}')
                return
            fileList.append(filePath)

        files.transferMocapData(*fileList)



    def __switchText(self, lineEditA, lineEditB):
        textA, textB= lineEditA.text(), lineEditB.text()
        lineEditA.setText(textB)
        lineEditB.setText(textA)


    def on_btn_matchData_clicked(self, args=None):
        if args == None:return
        dataTool.convertMocapData()


    def on_btn_saveMa_clicked(self, args=None):
        if args == None:return
        dataTool.saveNewData()
